import {action, observable} from "mobx";
import {BaseResInterface, sendGetRequest} from "../../../net";
import {productOrderItem} from "../../../page/primary/checkBill";
import {defaultArr} from "../../../util";
import * as _ from 'lodash';

interface CertResInterface extends BaseResInterface {
    data: Cert
}

interface Cert {

}

export class CertStore {
    @observable orderList: productOrderItem[] = defaultArr;

    @action.bound
    addOrderItem(item: productOrderItem) {
        const sameItemIndex = _.findIndex(this.orderList, function ({product, selectedSpecification}) {
            return product.id === item.product.id && selectedSpecification.id === item.selectedSpecification.id;
        });
        //是否已加入购物车
        if (sameItemIndex > -1) {
            return this.orderList[sameItemIndex].count += item.count;
        }
        this.orderList.push(item);
    }

    @action.bound
    removeOrderItem(item: productOrderItem) {
        const productIndex = _.findIndex(this.orderList, function ({product, selectedSpecification}) {
            return product.id === item.product.id && selectedSpecification.id === item.selectedSpecification.id;
        });
        this.orderList.splice(productIndex, 1);
    }

    updateOrderItemCount(item: productOrderItem, count: number) {
        const productIndex = _.findIndex(this.orderList, function ({product, selectedSpecification}) {
            return product.id === item.product.id && selectedSpecification.id === item.selectedSpecification.id;
        });
        this.orderList[productIndex].count = count;
    }

}

const CertStoreInstance = new CertStore();
export default CertStoreInstance